//
//  NSString+hrb.m
//  HRBDemo
//
//  Created by SZJ on 2020/12/2.
//  Copyright © 2020 ico. All rights reserved.
//

#import "NSString+hrb.h"

@implementation NSDecimalNumber (hrb)
/// 别看  看就是两个数 相加
- (NSDecimalNumber *(^)(NSDecimalNumber *))hrb_jia{
    NSDecimalNumber *(^a)(NSDecimalNumber *) = ^(NSDecimalNumber *ot){
        return [self decimalNumberByAdding:ot];
    };
    return a;
}
/// 别看  看就是两个数 相减
- (NSDecimalNumber *(^)(NSDecimalNumber *))hrb_jian{
    NSDecimalNumber *(^a)(NSDecimalNumber *) = ^(NSDecimalNumber *ot){
        return [self decimalNumberBySubtracting:ot];
    };
    return a;
}
/// 别看  看就是两个数 相乘
- (NSDecimalNumber *(^)(NSDecimalNumber *))hrb_cheng{
    NSDecimalNumber *(^a)(NSDecimalNumber *) = ^(NSDecimalNumber *ot){
        NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
                                              decimalNumberHandlerWithRoundingMode:NSRoundDown
                                              scale:8
                                              raiseOnExactness:NO
                                              raiseOnOverflow:NO
                                              raiseOnUnderflow:NO
                                              raiseOnDivideByZero:YES];
        return [[self decimalNumberByMultiplyingBy:ot] decimalNumberByRoundingAccordingToBehavior:roundUp];
    };
    return a;
}
/// 别看  看就是两个数 相除
- (NSDecimalNumber *(^)(NSDecimalNumber *))hrb_chu{
    NSDecimalNumber *(^a)(NSDecimalNumber *) = ^(NSDecimalNumber *ot){
        NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
                                              decimalNumberHandlerWithRoundingMode:NSRoundDown
                                              scale:8
                                              raiseOnExactness:NO
                                              raiseOnOverflow:NO
                                              raiseOnUnderflow:NO
                                              raiseOnDivideByZero:YES];
        return [[self decimalNumberByDividingBy:ot] decimalNumberByRoundingAccordingToBehavior:roundUp];
    };
    return a;
}
/// 别看  看就是前边是不是大于后边
- (BOOL(^)(NSDecimalNumber *))hrb_daYu{
    BOOL (^a)(NSDecimalNumber *) = ^(NSDecimalNumber *ot){
        return (BOOL)([self compare:ot] == NSOrderedDescending);
    };
    return a;
}
/// 别看  看就是前边是不是小于后边
- (BOOL(^)(NSDecimalNumber *))hrb_xiaoYu{
    BOOL (^a)(NSDecimalNumber *) = ^(NSDecimalNumber *ot){
        return (BOOL)([self compare:ot] == NSOrderedAscending);
    };
    return a;
}
/// 别看  看就是前边是不是等于后边
- (BOOL(^)(NSDecimalNumber *))hrb_dengYu{
    BOOL (^a)(NSDecimalNumber *) = ^(NSDecimalNumber *ot){
        return (BOOL)([self compare:ot] == NSOrderedSame);
    };
    return a;
}
@end

@implementation NSString (hrb)


NSDecimalNumber * _CountDecimal(id ot){
    NSDecimalNumber *otNum = nil;
    if ([ot isKindOfClass:NSString.class]) {
        otNum = [(NSString *)ot hrb_decimalNumer];
    }
    if ([ot isKindOfClass:NSNumber.class]) {
        otNum = [(NSNumber *)ot stringValue].hrb_decimalNumer;
    }
    if ([ot isKindOfClass:NSDecimalNumber.class]) {
        otNum = ot;
    }
    
    assert(otNum);
   
    return otNum;
}
- (NSString *(^)(id))hrb_jia{
    
    NSString *(^a)(id) = ^(id ot){
        return self.hrb_decimalNumer.hrb_jia(_CountDecimal(ot)).stringValue;
    };
    return a;
}

- (NSString *(^)(id))hrb_jian{
    NSString *(^a)(id) = ^(id ot){
        return self.hrb_decimalNumer.hrb_jian(_CountDecimal(ot)).stringValue;
    };
    return a;
}

- (NSString *(^)(id))hrb_cheng{
    NSString *(^a)(id) = ^(id ot){
        return self.hrb_decimalNumer.hrb_cheng(_CountDecimal(ot)).stringValue;
    };
    return a;
}

- (NSString *(^)(id))hrb_chu{
    NSString *(^a)(id) = ^(id ot){
        return self.hrb_decimalNumer.hrb_chu(_CountDecimal(ot)).stringValue;
    };
    return a;
}

- (BOOL(^)(id))hrb_daYu{
    BOOL (^a)(id) = ^(id ot){
        return self.hrb_decimalNumer.hrb_daYu(_CountDecimal(ot));
    };
    return a;
}

- (BOOL(^)(id))hrb_xiaoYu{
    BOOL (^a)(id) = ^(id ot){
        return self.hrb_decimalNumer.hrb_xiaoYu(_CountDecimal(ot));
    };
    return a;
}

- (BOOL(^)(id))hrb_dengYu{
    BOOL (^a)(id) = ^(id ot){
        return self.hrb_decimalNumer.hrb_dengYu(_CountDecimal(ot));
    };
    return a;
}


- (NSString *)hrb_half{
    return self.hrb_chu(@"2");
}

- (NSString *)hrb_OneThird{
    return self.hrb_chu(@"3");
}

- (NSString *)hrb_quarter{
    return self.hrb_chu(@"4");
}

- (NSString *)hrb_reciprocal{
    return @"1".hrb_chu(self);
}

- (NSDecimalNumber *)hrb_decimalNumer{
    return [NSDecimalNumber decimalNumberWithString:self];
}
- (NSString *)hrb_removeMoreZero{
    NSString *newSelf = self;
    if ([newSelf containsString:@"."]) {
        while (newSelf.hrb_lastChar.hrb_isEqual(@"0")) {
            newSelf = newSelf.hrb_removeLastChar;
        }
        if (newSelf.hrb_lastChar.hrb_isEqual(@".")) {
            newSelf = newSelf.hrb_removeLastChar;
        }
    }
    return newSelf;
}
- (NSString *)hrb_removeLastChar{
    if (!self.length)return self;
    return [self substringToIndex:self.length - 1];
}
    
- (NSString *)hrb_lastChar{
    return [self substringFromIndex:self.length - 1];
}
- (BOOL(^)(NSString *))hrb_isEqual{
    BOOL(^a)(NSString *) = ^(NSString *str){
        return [self isEqualToString:str];
    };
    return a;
}
- (id)hrb_jsonDecode{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    id value = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        NSLog(@"jsonValueDecoded error:%@", error);
    }
    return value;
}
- (void(^)(id target,SEL action))hrb_addListener{
    void(^a)(id target,SEL action) = ^(id target,SEL action){
        [NSNotificationCenter.defaultCenter addObserver:target selector:action name:self object:nil];
    };
    return a;
}
- (void(^)(id object))hrb_postToListener{
    void(^a)(id object) = ^(id object){
        [NSNotificationCenter.defaultCenter postNotificationName:self object:object userInfo:nil];
    };
    return a;
}
- (void(^)(id data))hrb_save{
    void(^a)(id data) = ^(id data){
        [NSUserDefaults.standardUserDefaults setObject:data forKey:self];
        [NSUserDefaults.standardUserDefaults synchronize];
    };
    return a;
}

- (id(^)(void))hrb_read{
    id(^a)(void) = ^{
        return [NSUserDefaults.standardUserDefaults objectForKey:self];
    };
    return a;
}

- (NSString *(^)(NSString *))add{
    NSString *(^a)(NSString *) = ^(NSString *b){
        return [self stringByAppendingString:b];
    };
    return a;
}

@end
